//
//  EWBusinessInfoCell.m
//  haveSomethingToEat
//
//  Created by 陈 宁权 on 17/6/22.
//  Copyright © 2017年 陈 宁权. All rights reserved.
//

#import "EWBusinessInfoCell.h"
#import "DefaultPortraitView.h"
#import "XHStarRateView.h"

@interface EWBusinessInfoCell()

@property (nonatomic,strong) UIImageView *logoView;
@property (nonatomic,strong) UILabel *businessName;
@property (nonatomic,strong) XHStarRateView *starRateView;
@property (nonatomic,strong) UILabel *monthOrders;
@property (nonatomic,strong) UILabel *distance;
@end

@implementation EWBusinessInfoCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"EWBusinessInfoCell";
    // 1.缓存中取
    EWBusinessInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // 2.创建
    if (cell == nil) {
        cell = [[EWBusinessInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupChildViews];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setupChildViews
{
    UIView *backGroundView = [[UIView alloc] init];
    backGroundView.layer.cornerRadius = 15.0;
    backGroundView.layer.masksToBounds = YES;
    backGroundView.backgroundColor = BASE_COLOR;
    [self addSubview:backGroundView];
    
    _logoView = [[UIImageView alloc] init];
    _logoView.layer.cornerRadius = 5.0;
    _logoView.layer.masksToBounds = YES;
    [backGroundView addSubview:_logoView];
    
    _businessName = [[UILabel alloc] init];
    _businessName.textColor = [UIColor whiteColor];
    [backGroundView addSubview:_businessName];
    
    _starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(0, 0, 40, 15)];
    _starRateView.isAnimation = YES;
    _starRateView.rateStyle = IncompleteStar;
    _starRateView.tag = 1;
    [backGroundView addSubview:_starRateView];
    
    _monthOrders = [[UILabel alloc] init];
    _monthOrders.font = [UIFont systemFontOfSize:12];
    _monthOrders.textColor = [UIColor whiteColor];
    [backGroundView addSubview:_monthOrders];
    
    _distance = [[UILabel alloc] init];
    _distance.textColor = [UIColor whiteColor];
    _distance.textAlignment = NSTextAlignmentRight;
    _distance.font = [UIFont systemFontOfSize:12];
    [backGroundView addSubview:_distance];
    
    [backGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.bottom.equalTo(self).offset(-2);
        make.left.equalTo(self).offset(2);
        make.right.equalTo(self).offset(-2);
    }];
    
    [_logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.bottom.equalTo(self).offset(-10);
        make.left.equalTo(self).offset(10);
        make.width.offset(60);
    }];
    
    [_businessName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(_logoView.mas_right).offset(5);
        make.right.equalTo(self).offset(-5);
        make.height.offset(30);
    }];
    
    [_starRateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_businessName.mas_bottom).offset(5);
        make.left.equalTo(_logoView.mas_right).offset(5);
        make.width.offset(40);
        make.height.offset(15);
    }];
    
    [_monthOrders mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_starRateView);
        make.left.equalTo(_starRateView.mas_right).offset(5);
        make.width.offset(100);
        make.height.offset(15);
    }];
    
    [_distance mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_monthOrders);
        make.left.equalTo(_monthOrders.mas_right).offset(5);
        make.right.equalTo(backGroundView).offset(-5);
        make.height.offset(15);
    }];
}


- (void)setBusinessInfo:(EWBusinessInfo *)businessInfo
{
    _businessInfo = businessInfo;
    
    if (_businessInfo.portraitUri.length < 1) {
        DefaultPortraitView *defaultPortrait =
        [[DefaultPortraitView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [defaultPortrait setColorAndLabel:self.businessInfo.businessId
                                 Nickname:self.businessInfo.businessName];
        UIImage *portrait = [defaultPortrait imageFromView];
        _logoView.image = portrait;
    }
    else
    {
        [_logoView sd_setImageWithURL:[NSURL URLWithString:_businessInfo.portraitUri]];
    }
    
    _businessName.text = _businessInfo.businessName;
    _starRateView.currentScore = [_businessInfo.score floatValue];
    _monthOrders.text = [NSString stringWithFormat:@"月订单%@",_businessInfo.orders];
    
    CLLocation *businessLocate = [[CLLocation alloc] initWithLatitude:[_businessInfo.latitude doubleValue] longitude:[_businessInfo.longitude doubleValue]];
    
    if (![[BQLocationManager sharedManager] haveLocationAuth]) {
        _distance.text = @"请开启定位权限";
    }
    else{
        [[BQLocationManager sharedManager] startUpdatingLocation];
        CLLocationDistance kilometers=[[BQLocationManager sharedManager].location distanceFromLocation:businessLocate]/1000;
        if (kilometers < 1) {
            _distance.text = [NSString stringWithFormat:@"距您%.fm",kilometers * 1000];
        }
        else{
            _distance.text = [NSString stringWithFormat:@"距您%.1fkm",kilometers];
        }
    }
    
}

@end
